﻿using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000051 RID: 81
    public static class EnjoyPayProxy {
        // Token: 0x060002A6 RID: 678 RVA: 0x00003B2C File Offset: 0x00001D2C
        public static void AddEnjoyPayHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/EnjoyRMIS_WS/WS/Alipay/CWSAlipay.asmx", new HttpContextActionDelegate(Process));
            svr.RouterList.Add("POST/EnjoyRMIS_WS/wcf/wcfForPos30.svc/json/ProcPay", new HttpContextActionDelegate(ProcessJson));
        }

        // Token: 0x060002A7 RID: 679 RVA: 0x0000D98C File Offset: 0x0000BB8C
        private static void ProcessJson(HttpContext ctx) {
            EnjoyPayResult enjoyPayResult = new EnjoyPayResult {
                result_code = EnjoyPayResultCode.const_2.ToString()
            };
            try {
                string json = new StreamReader(ctx.Request).ReadToEnd();
                EnjoyPayRequest enjoyPayRequest = json.JsonDecode<EnjoyPayRequest>();
                HongshPayTrade hongshPayTrade;
                switch (enjoyPayRequest.sOperCode) {
                    case 1: {
                            TradeAuthPaymentRequest tradeAuthPaymentRequest = new TradeAuthPaymentRequest {
                                Amount = (int)(enjoyPayRequest.sAmount * 100m),
                                Authcode = enjoyPayRequest.sBarcode,
                                ClientTradeId = enjoyPayRequest.sOutTradeNo
                            };
                            try {
                                if (enjoyPayRequest.goods_detail != null && enjoyPayRequest.goods_detail.Length != 0) {
                                    tradeAuthPaymentRequest.GoodsList = (from d in enjoyPayRequest.goods_detail
                                                                         select new TradeGoods {
                                                                             Id = d.goodsId,
                                                                             Name = d.goodsName,
                                                                             Price = (int)Math.Ceiling(d.price * 100m),
                                                                             Quantity = d.quantity
                                                                         }).ToArray<TradeGoods>();
                                }
                            }
                            catch {
                            }
                            hongshPayTrade = ctx.Execute(tradeAuthPaymentRequest);
                            break;
                        }
                    case 2: {
                            TradePaymentQueryRequest req = new TradePaymentQueryRequest {
                                ClientTradeId = enjoyPayRequest.sOutTradeNo
                            };
                            hongshPayTrade = ctx.Execute(req);
                            break;
                        }
                    case 3: {
                            TradeRefundRequest req2 = new TradeRefundRequest {
                                ClientRefundTradeId = enjoyPayRequest.sOutRefundNo,
                                ClientTradeId = enjoyPayRequest.sOutTradeNo,
                                Amount = (int)Math.Ceiling(enjoyPayRequest.sAmount * 100m),
                                RefundAmount = (int)Math.Ceiling(enjoyPayRequest.sRefundAmount * 100m)
                            };
                            hongshPayTrade = ctx.Execute(req2);
                            break;
                        }
                    default:
                        throw new Exception("不支持的方法！");
                }
                enjoyPayResult.result_detail = hongshPayTrade.StateDescript;
                TradeState state = hongshPayTrade.State;
                TradeState tradeState = state;
                if (tradeState != TradeState.未知) {
                    if (tradeState == TradeState.成功) {
                        enjoyPayResult.result_detail = "成功";
                        enjoyPayResult.cash_fee = (hongshPayTrade.Amount / 100m).ToString("0.00");
                        enjoyPayResult.open_id = hongshPayTrade.Payer;
                        enjoyPayResult.result_code = EnjoyPayResultCode.const_1.ToString();
                    }
                }
                else {
                    enjoyPayResult.result_code = EnjoyPayResultCode.const_0.ToString();
                }
                switch (hongshPayTrade.Payway) {
                    case TradePayway.微信:
                        enjoyPayResult.paymethod = "wx";
                        break;
                    case TradePayway.支付宝:
                        enjoyPayResult.paymethod = "zfb";
                        break;
                    case TradePayway.云闪付:
                        enjoyPayResult.paymethod = "ylysf";
                        break;
                }
                enjoyPayResult.trade_no = hongshPayTrade.TradeId;
                enjoyPayResult.out_trade_no = enjoyPayRequest.sOutTradeNo;
                enjoyPayResult.out_refund_no = enjoyPayRequest.sOutRefundNo;
            }
            catch (Exception ex) {
                enjoyPayResult.result_detail = ex.Message;
            }
            ctx.Result = new HttpJsonResult(new {
                Result = new {
                    Records = new EnjoyPayResult[]
                    {
                        enjoyPayResult
                    },
                    Result = new {
                        Code = 0,
                        Info = string.Empty
                    }
                }
            });
        }

        // Token: 0x060002A8 RID: 680 RVA: 0x0000DC98 File Offset: 0x0000BE98
        private static void Process(HttpContext ctx) {
            string input = new StreamReader(ctx.Request).ReadToEnd();
            string text = ctx.Request.Header["SOAPAction"];
            if (text.Equals("\"http://tempuri.org/BarcodeTrade\"", StringComparison.OrdinalIgnoreCase)) {
                text = "BarcodeTrade";
            }
            else if (text.Equals("\"http://tempuri.org/QueryTrade\"", StringComparison.OrdinalIgnoreCase)) {
                text = "QueryTrade";
            }
            else {
                ctx.Result = HttpResult.NoFound;
            }
            if (ctx.Result == null) {
                EnjoyPayRequest enjoyPayRequest = Regex.Match(input, "(?<=<ns1:RequestData>)(.*?)(?=&#xA;</ns1:RequestData>)").Value.JsonDecode<EnjoyPayRequest>();
                EnjoyPayResult enjoyPayResult = new EnjoyPayResult {
                    out_trade_no = enjoyPayRequest.sOutTradeNo
                };
                try {
                    HongshPayTrade hongshPayTrade = null;
                    if (text == "BarcodeTrade") {
                        TradeAuthPaymentRequest tradeAuthPaymentRequest = new TradeAuthPaymentRequest {
                            Amount = (int)(enjoyPayRequest.sAmount / 100m),
                            Authcode = enjoyPayRequest.sBarcode,
                            ClientTradeId = enjoyPayRequest.sOutTradeNo
                        };
                        tradeAuthPaymentRequest.GoodsList = (from d in enjoyPayRequest.goods_detail
                                                             select new TradeGoods {
                                                                 Id = d.goodsId,
                                                                 Name = d.goodsName,
                                                                 Price = (int)Math.Ceiling(d.price * 100m),
                                                                 Quantity = d.quantity
                                                             }).ToArray<TradeGoods>();
                        hongshPayTrade = ctx.Execute(tradeAuthPaymentRequest);
                    }
                    if (hongshPayTrade == null || hongshPayTrade.State == TradeState.未知) {
                        hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                            ClientTradeId = enjoyPayRequest.sOutTradeNo
                        });
                    }
                    if (hongshPayTrade.State == TradeState.成功) {
                        enjoyPayResult.result_detail = "交易成功";
                        enjoyPayResult.result_code = EnjoyPayResultCode.const_1.ToString();
                    }
                    else {
                        enjoyPayResult.result_detail = "等待用户确认中";
                    }
                    switch (hongshPayTrade.Payway) {
                        case TradePayway.微信:
                            enjoyPayResult.paymethod = "wx";
                            break;
                        case TradePayway.支付宝:
                            enjoyPayResult.paymethod = "zfb";
                            break;
                        case TradePayway.云闪付:
                            enjoyPayResult.paymethod = "ylysf";
                            break;
                    }
                    enjoyPayResult.cash_fee = hongshPayTrade.Amount.ToString();
                    enjoyPayResult.open_id = hongshPayTrade.Payer;
                }
                catch (Exception ex) {
                    enjoyPayResult.result_code = EnjoyPayResultCode.const_2.ToString();
                    enjoyPayResult.out_trade_no = enjoyPayRequest.sOutTradeNo;
                    enjoyPayResult.result_detail = ex.Message;
                }
                StringBuilder stringBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").Append("<soap:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">").Append("<soap:Body>").AppendFormat("<{0}Response xmlns=\"http://tempuri.org/\">", text).AppendFormat("<{0}Result>", text).Append(enjoyPayResult.JsonEncode(Formatting.None)).AppendFormat("</{0}Result>", text).AppendFormat("</{0}Response>", text).Append("</soap:Body>").Append("</soap:Envelope>");
                ctx.Result = new HttpTextResult(stringBuilder.ToString(), "application/soap+xml");
            }
        }

        // Token: 0x02000052 RID: 82
        private enum EnjoyPayResultCode {
            // Token: 0x04000122 RID: 290
            const_0,
            // Token: 0x04000123 RID: 291
            const_1,
            // Token: 0x04000124 RID: 292
            const_2
        }

        // Token: 0x02000053 RID: 83
        private class EnjoyPayRequest {
            // Token: 0x17000125 RID: 293
            // (get) Token: 0x060002A9 RID: 681 RVA: 0x00003B66 File Offset: 0x00001D66
            // (set) Token: 0x060002AA RID: 682 RVA: 0x00003B6E File Offset: 0x00001D6E
            public int sOperCode { get; set; }

            // Token: 0x17000126 RID: 294
            // (get) Token: 0x060002AB RID: 683 RVA: 0x00003B77 File Offset: 0x00001D77
            // (set) Token: 0x060002AC RID: 684 RVA: 0x00003B7F File Offset: 0x00001D7F
            public string sBarcode { get; set; }

            // Token: 0x17000127 RID: 295
            // (get) Token: 0x060002AD RID: 685 RVA: 0x00003B88 File Offset: 0x00001D88
            // (set) Token: 0x060002AE RID: 686 RVA: 0x00003B90 File Offset: 0x00001D90
            public decimal sAmount { get; set; }

            // Token: 0x17000128 RID: 296
            // (get) Token: 0x060002AF RID: 687 RVA: 0x00003B99 File Offset: 0x00001D99
            // (set) Token: 0x060002B0 RID: 688 RVA: 0x00003BA1 File Offset: 0x00001DA1
            public decimal sRefundAmount { get; set; }

            // Token: 0x17000129 RID: 297
            // (get) Token: 0x060002B1 RID: 689 RVA: 0x00003BAA File Offset: 0x00001DAA
            // (set) Token: 0x060002B2 RID: 690 RVA: 0x00003BB2 File Offset: 0x00001DB2
            public string sOutRefundNo { get; set; }

            // Token: 0x1700012A RID: 298
            // (get) Token: 0x060002B3 RID: 691 RVA: 0x00003BBB File Offset: 0x00001DBB
            // (set) Token: 0x060002B4 RID: 692 RVA: 0x00003BC3 File Offset: 0x00001DC3
            public string sOutTradeNo { get; set; }

            // Token: 0x1700012B RID: 299
            // (get) Token: 0x060002B5 RID: 693 RVA: 0x00003BCC File Offset: 0x00001DCC
            // (set) Token: 0x060002B6 RID: 694 RVA: 0x00003BD4 File Offset: 0x00001DD4
            public string sSubject { get; set; }

            // Token: 0x1700012C RID: 300
            // (get) Token: 0x060002B7 RID: 695 RVA: 0x00003BDD File Offset: 0x00001DDD
            // (set) Token: 0x060002B8 RID: 696 RVA: 0x00003BE5 File Offset: 0x00001DE5
            public string sTerminalNo { get; set; }

            // Token: 0x1700012D RID: 301
            // (get) Token: 0x060002B9 RID: 697 RVA: 0x00003BEE File Offset: 0x00001DEE
            // (set) Token: 0x060002BA RID: 698 RVA: 0x00003BF6 File Offset: 0x00001DF6
            public EnjoyGoods[] goods_detail { get; set; }

            // Token: 0x060002BB RID: 699 RVA: 0x0000286D File Offset: 0x00000A6D
            public EnjoyPayRequest() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000054 RID: 84
        private class EnjoyPayResult {
            // Token: 0x1700012E RID: 302
            // (get) Token: 0x060002BC RID: 700 RVA: 0x00003BFF File Offset: 0x00001DFF
            // (set) Token: 0x060002BD RID: 701 RVA: 0x00003C07 File Offset: 0x00001E07
            public string result_code { get; set; }

            // Token: 0x1700012F RID: 303
            // (get) Token: 0x060002BE RID: 702 RVA: 0x00003C10 File Offset: 0x00001E10
            // (set) Token: 0x060002BF RID: 703 RVA: 0x00003C18 File Offset: 0x00001E18
            public string result_detail { get; set; }

            // Token: 0x17000130 RID: 304
            // (get) Token: 0x060002C0 RID: 704 RVA: 0x00003C21 File Offset: 0x00001E21
            // (set) Token: 0x060002C1 RID: 705 RVA: 0x00003C29 File Offset: 0x00001E29
            public string out_trade_no { get; set; }

            // Token: 0x17000131 RID: 305
            // (get) Token: 0x060002C2 RID: 706 RVA: 0x00003C32 File Offset: 0x00001E32
            // (set) Token: 0x060002C3 RID: 707 RVA: 0x00003C3A File Offset: 0x00001E3A
            public string out_refund_no { get; set; }

            // Token: 0x17000132 RID: 306
            // (get) Token: 0x060002C4 RID: 708 RVA: 0x00003C43 File Offset: 0x00001E43
            // (set) Token: 0x060002C5 RID: 709 RVA: 0x00003C4B File Offset: 0x00001E4B
            public string trade_no { get; set; }

            // Token: 0x17000133 RID: 307
            // (get) Token: 0x060002C6 RID: 710 RVA: 0x00003C54 File Offset: 0x00001E54
            public string c_trade_no {
                get {
                    return trade_no;
                }
            }

            // Token: 0x17000134 RID: 308
            // (get) Token: 0x060002C7 RID: 711 RVA: 0x00003C54 File Offset: 0x00001E54
            public string sFlowNo {
                get {
                    return trade_no;
                }
            }

            // Token: 0x17000135 RID: 309
            // (get) Token: 0x060002C8 RID: 712 RVA: 0x00003C5C File Offset: 0x00001E5C
            // (set) Token: 0x060002C9 RID: 713 RVA: 0x00003C64 File Offset: 0x00001E64
            public string open_id { get; set; }

            // Token: 0x17000136 RID: 310
            // (get) Token: 0x060002CA RID: 714 RVA: 0x00003C6D File Offset: 0x00001E6D
            public string buyer_user_id {
                get {
                    return open_id;
                }
            }

            // Token: 0x17000137 RID: 311
            // (get) Token: 0x060002CB RID: 715 RVA: 0x00003C6D File Offset: 0x00001E6D
            public string buyer_login_id {
                get {
                    return open_id;
                }
            }

            // Token: 0x17000138 RID: 312
            // (get) Token: 0x060002CC RID: 716 RVA: 0x00003C75 File Offset: 0x00001E75
            // (set) Token: 0x060002CD RID: 717 RVA: 0x00003C7D File Offset: 0x00001E7D
            public string cash_fee { get; set; }

            // Token: 0x17000139 RID: 313
            // (get) Token: 0x060002CE RID: 718 RVA: 0x00003C86 File Offset: 0x00001E86
            // (set) Token: 0x060002CF RID: 719 RVA: 0x00003C8E File Offset: 0x00001E8E
            public string paymethod { get; set; }

            // Token: 0x060002D0 RID: 720 RVA: 0x0000286D File Offset: 0x00000A6D
            public EnjoyPayResult() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000055 RID: 85
        private class EnjoyGoods {
            // Token: 0x1700013A RID: 314
            // (get) Token: 0x060002D1 RID: 721 RVA: 0x00003C97 File Offset: 0x00001E97
            // (set) Token: 0x060002D2 RID: 722 RVA: 0x00003C9F File Offset: 0x00001E9F
            public string goodsId { get; set; }

            // Token: 0x1700013B RID: 315
            // (get) Token: 0x060002D3 RID: 723 RVA: 0x00003CA8 File Offset: 0x00001EA8
            // (set) Token: 0x060002D4 RID: 724 RVA: 0x00003CB0 File Offset: 0x00001EB0
            public string goodsName { get; set; }

            // Token: 0x1700013C RID: 316
            // (get) Token: 0x060002D5 RID: 725 RVA: 0x00003CB9 File Offset: 0x00001EB9
            // (set) Token: 0x060002D6 RID: 726 RVA: 0x00003CC1 File Offset: 0x00001EC1
            public decimal price { get; set; }

            // Token: 0x1700013D RID: 317
            // (get) Token: 0x060002D7 RID: 727 RVA: 0x00003CCA File Offset: 0x00001ECA
            // (set) Token: 0x060002D8 RID: 728 RVA: 0x00003CD2 File Offset: 0x00001ED2
            public double quantity { get; set; }

            // Token: 0x060002D9 RID: 729 RVA: 0x0000286D File Offset: 0x00000A6D
            public EnjoyGoods() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }
    }
}
